home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Handling complex numbers...
- Date: 12 Mar 1996 09:25:52 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4i4c30INNs95@keats.ugrad.cs.ubc.ca>
- References: <4hi113$2i8k@mercury.cc.uottawa.ca> <larry_kearney-0803960747220001@amaryllisp1.appsig.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <larry_kearney-0803960747220001@amaryllisp1.appsig.com>,
- Larry Kearney <larry_kearney@appsig.com> wrote:
- >> Dear fellow netters,
- >>
- >> Can someone kindly explain to me how to represent complex numbers in C?
- >> I am writing a numerical method program to calculate the area under
- >> a curve using Simpson's Rule. The problem that I face right now is
- >> the representation of "i" (where i^2 = -1) in my C program.
- >>
- >> Here's a simple example of what I mean:
- >>
- >> _b
- >> |
- >> | exp(ix) dx
- >> _|
- >> a
- >>
- >>
- >> How do I implement this function exp(ix)?
- >>
- >> I'm grateful for your help.
- >>
- >> Charles Tran
- >> --
- >> Charles
- >
- >The C language is not terribly efficient when it comes to complex numbers.
-
- Correction: your code below is not terribly efficient. You can use macros to
- represent common complex operations. These can be as good as C++ inline
- member functions, for instance.
-
- >You can represent a complex number as a struct, i.e.,
- >
- >typedef struct
- >{
- > double re; /* real part of the complex number */
- > double im; /* imaginary part of the complex number */
- >} complex;
-
- >but the big problem is that the arithmetic operators don't know what to do
- >when presented with values that are structures. As a result, the
- >programmer is required to implement functions that perform that standard
- >operations and that take complex structs as arguments. For example,
- >
- >void AddComplex ( complex *a, complex *b, complex *c )
- >{
- > c->real = a->real + b->real;
- > c->imag = a->imag + b->imag;
- >}
- >
- >This makes coding an expressions such as
- >
- > x = 2.0 * ( y + z ) / d /* all variables are complex */
- >
- >an exercise is calling functions and the creation of lots of temporary
- >complex arguments.
-
- Many quality C compilers will inline such small functions, provided they have
- internal linkage.
-
- You have never used pre-processor macros to do inline code? Here is a
- two-operand form of the addition, though you could do a C <- A + B
- three-operand form this way as well:
-
- #define AddComplex(A,B) ((A)->real += (B)->real, (A)->imag += (B)->imag)
-
- Here, a comma expression is used to carry out two assignment expressions left
- to right. The ``return value'' of the macro is the imaginary part of the
- result (i.e. the result of the rightmost expression).
-
- >Alternate
- >solutions would be to attempt to code your function using either Fortran, where
- >complex arithmetic is built-in (does anybody know that anymore) or code it
- >in C++ where you can easily create a complex class and then write function
- >that overrides the normal arithmetic operators and trig functions when
- >complex arithmetic is used. This approach would allow a more natural
- >implementation of expressions.
-
- C++ operator redefinition is just ``syntactic sugar'' (in the words of the
- comp.lang.c++ FAQ) to hide a function call (which can be inlined, of course).
- It's not any more efficient than the above macros, though arguably more
- elegant.
- --
-
-